//
//  WBMainViewController.swift
//  CCWeibo
//
//  Created by Chad on 2016/11/4.
//  Copyright © 2016年 Chad. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    setupChildControllers()
    setupComposeButton()
        
        
        
    }
    
    //FIXME: 添加按钮功能实现
        func composeStatus() {
            print ("添加按钮功能实现")
        }
    
    lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    //lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "")
}

 
extension WBMainViewController {
    
    func setupComposeButton() {
        tabBar.addSubview(composeButton)
        //composeButton.center = tabBar.center
        composeButton.center.x = tabBar.bounds.width / 2
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }

    /// <#Description#>
    //private func setupChildController() {
    func setupChildControllers() {
  
        let array = [
            ["clsName":"WBHomeViewController", "title" :"首页" , "imageName" : "home"],
            ["clsName":"WBMessageViewController", "title" :"消息" , "imageName" : "message_center"],
            ["clsName":"viewController"],
            ["clsName":"WBDiscoverViewController", "title" :"发现" , "imageName" : "discover"],
            ["clsName":"WBProfileViewController", "title" :"我" , "imageName" : "profile"]
        ]
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
        
        
    }
    
    /// 使用字典创建一个自控制器
    ///
    /// - Parameter dict: 信息字典[clsName, title, imageName]
    /// - Returns: 子控制器
    private func controller(dict: [String: String]) -> UIViewController {
        //取得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
            return UIViewController()
        }
        
        //创建视图控制器
        let vc = cls.init()
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named:  "tabbar_" + imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        //vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: UIControlState(rawValue:0))
        
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }
    
}


